﻿var savestrTcp = [];
var sDateTcp;
var eDateTcp;
var iTcp;
var FindCfmTcp;
var SearchTcpId;
var SearchTcpArea;
var SearchTcpDefine;
var strTcp;
var childGridKey;
var flag;
$(document).ready(function() {
	var session = sessionStorage.getItem("session");
	if (!session) {
		window.parent.location = "../../login.html";
	}
	$('#sDateTcpPing').datetimebox('setValue', ThreeDayBefore);

	$("input", $("#DeviceIdTcp").next("span")).click(function() {
		$('#QueryTcpPingTab').window('open');
	});

	$('#gridbox').width($(window).width());
	SearchTcpId = function() {
		var aDeviceidTcp;
		savestrTcp = [""];
		flag = 1;
		aDeviceidTcp = $("#QueryTcpPingId").jqGrid('getGridParam', 'selarrrow'); //获取选中行号	
		for (iTcp = 0; iTcp < aDeviceidTcp.length; iTcp++) {
			savestrTcp[iTcp] = $("#QueryTcpPingId").getCell(aDeviceidTcp[iTcp], 'deviceid');
		}
		$("#DeviceIdTcp").textbox('setValue', savestrTcp);
		$('#QueryTcpPingTab').window('close');
		//		count: $("#jqGrid").getCell(rowKey, "count"),
	}
	SearchTcpArea = function() {
		var aAreaTcp;
		savestrTcp = [""];
		flag = 2;
		aAreaTcp = $("#QueryTcpPingArea").jqGrid('getGridParam', 'selarrrow'); //获取选中行号		
		for (iTcp = 0; iTcp < aAreaTcp.length; iTcp++) {
			savestrTcp[iTcp] = $("#QueryTcpPingArea").getCell(aAreaTcp[iTcp], 'text');
		}
		console.log(savestrTcp);
		$("#DeviceIdTcp").textbox('setValue', savestrTcp);
		$('#QueryTcpPingTab').window('close');
		//		count: $("#jqGrid").getCell(rowKey, "count"),
	}
	SearchTcpDefine = function() {
		var aDefineTcp;
		savestrTcp = [""];
		flag = 3;
		aDefineTcp = $("#QueryTcpPingDefine").jqGrid('getGridParam', 'selarrrow'); //获取选中行号		
		for (iTcp = 0; iTcp < aDefineTcp.length; iTcp++) {
			savestrTcp[iTcp] = $("#QueryTcpPingDefine").getCell(aDefineTcp[iTcp], 'probeGroup');
		}
		console.log(savestrTcp);
		$("#DeviceIdTcp").textbox('setValue', savestrTcp);
		$('#QueryTcpPingTab').window('close');
		//		count: $("#jqGrid").getCell(rowKey, "count"),
	}
	$("#QueryTcpPingGrid").jqGrid({
		colModel: [{
			label: '识别码',
			name: 'uuid'
		}, {
			label: '任务编号',
			name: 'taskid'
		}, {
			label: '设备类型',
			name: 'type'
		}, {
			label: '执行时间',
			name: 'executedate'
		}, {
			label: '执行耗时',
			name: 'executetimespent'
		}, {
			label: '服务器',
			name: 'server'
		}, {
			label: 'IP地址',
			name: 'ipaddress'
		}, {
			label: '端口',
			name: 'port'
		}, {
			label: '丢包率（%）',
			name: 'losspercentage'
		}, {
			label: '发送数',
			name: 'sentcount'
		}, {
			label: '总耗时',
			name: 'totaltime'
		}, {
			label: '包大小',
			name: 'packetsize'
		}, {
			label: '接收数',
			name: 'recvcount'
		}, {
			label: '最小延时',
			name: 'minlatency'
		}, {
			label: '标准偏差延时',
			name: 'stddevlatency'
				//			width: 96
		}, {
			label: '最大延时',
			name: 'maxlatency'
				//			width: 96
		}, {
			label: '平均延时',
			name: 'avglatency'
				//			width: 96
		}],
		viewrecords: true,
		height: 480,
		autowidth: true,
		pagerpos: 'center',
		rowNum: 17,
		//		autowidth:true,
		loadonce: true,
		page: 1,
		subGrid: true, // set the subGrid property to true to show expand buttons for each row
		subGridRowExpanded: showChildGrid, // javascript function that will take care of showing the child grid
		loadtext: '正在加载数据',
		pager: "#QueryTcpPingGridPager",
		sortorder: "asc",
		sortname: 'id'
	});

	$(window).bind('resize', function() {
		$("#QueryTcpPingGrid").setGridWidth($(window).width() * 1);
		$("#QueryTcpPingGrid").setGridHeight($(window).height() - 110);
	});

	ExportFile = function() {
		$('#QueryTcpPingGrid').jqGrid('setGridParam', {
			rowNum: 1000,
		}).trigger('reloadGrid');
		var title = "TcpPing测试报告";
		getXlsFromTbl('QueryTcpPingGrid', 'gridtable', title, true);
		setTimeout(BackGrid(), 500);

		function BackGrid() {
			$('#QueryTcpPingGrid').jqGrid('setGridParam', {
				rowNum: 17,
			}).trigger('reloadGrid');
		}
	}

	$('#QueryTcpPingGrid').navGrid("#QueryTcpPingGridPager", {
		search: false, // show search button on the toolbar
		add: false,
		edit: false,
		del: false,
		refresh: true
	});

	FindCfmTcp = function() {
		sDateTcp = $('#sDateTcpPing').datetimebox('getValue').substr(0, 10);
		eDateTcp = $('#eDateTcpPing').datetimebox('getValue').substr(0, 10);
		if (DateRight(sDateTcp, eDateTcp)) {
			strTcp = savestrTcp;
			if ((sDateTcp != "") && (eDateTcp != "") && (strTcp != "")) {
				var myurl = IP + "QueryTcpPingData?callback=?&session=" + session + "&flag=" + flag + "&savestr=" + strTcp + "&sDate=" + sDateTcp + "&eDate=" + eDateTcp;
				console.log(myurl);
				//		alert(myurl);
				$('#QueryTcpPingGrid').jqGrid('setGridParam', {
					url: myurl,
					type: 'Get',
					datatype: "jsonp",
				}).trigger('reloadGrid');
				insertopertlog('拨测报告', 'TcpPing测试报告', '查询', '', '');
			} else {
				$.messager.show({
					title: '提示！',
					msg: '请选择设备信息',
					showType: 'fade',
					style: {
						right: '',
						bottom: ''
					}
				})
			}
		} else {
			$.messager.show({
				title: '提醒',
				msg: '请选择正确的时间范围！',
				timeout: 1300,
				showType: 'slide',
				style: {
					right: '',
					bottom: ''
				}
			});
		}
	}

	var filter;
	jQuery("#QueryTcpPingId").jqGrid({
		url: IP + "GetProbeInfoList?callback=?&session=" + session,
		datatype: "jsonp",
		height: 175,
		width: 383,
		colNames: ['设备名称', '设备唯一编码'],
		colModel: [{
			name: 'deviceName',
			index: 'deviceName',
		}, {
			name: 'deviceid',
			index: 'deviceid',
			sopt: ['eq']
		}],
		loadonce: true,
		mtype: "GET",
		multiselect: true,
		multiselectWidth: 80,
		page: 1,
		viewrecords: true
	});

	var timer;
	$("#SearchCellTcpPingId").on("keyup", function() {

		var self = this;
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(function() {
			//timer = null;
			$("#QueryTcpPingId").jqGrid('filterInput', self.value);
		}, 0);
	});

	$("#QueryTcpPingArea").jqGrid({
		url: IP + "GetAreaInfoList?callback=?&session=" + session,
		datatype: "jsonp",
		height: 175,
		width: 383,
		colModel: [{
			label: '县市',
			name: 'text',
			sopt: ['eq'],
			width: 125
		}],
		//		rowNum: 1000,		
		loadonce: true,
		mtype: "GET",
		multiselect: true,
		multiselectWidth: 80,
		//		rownumWidth: 40,
		//rowNum: 8,
		gridview: true,
		page: 1,
		viewrecords: true
			//		sortorder: "asc",
			//		sortname: 'id'
	});
	$("#SearchCellTcpPingArea").on("keyup", function() {

		var self = this;
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(function() {
			//timer = null;
			$("#QueryTcpPingArea").jqGrid('filterInput', self.value);
		}, 0);
	});


	$("#QueryTcpPingDefine").jqGrid({
		url: IP + "GetProbeGroupInfo?callback=?&session=" + session,
		mtype: "Get",
		datatype: "jsonp",
		colModel: [{
			label: '探针组',
			name: 'probeGroup',
			sopt: ['eq'],
			width: 50
		}, {
			label: '创建人',
			name: 'cPreson',
			sopt: ['eq'],
			width: 50
		}],
		loadonce: true,
		multiselect: true,
		multiselectWidth: 80,
		viewrecords: true,
		height: 175,
		width: 383,
		//			rowNum: 8,
		page: 1,
		loadtext: '正在加载数据',
	});
	$("#SearchCellPingTcpDefine").on("keyup", function() {

		var self = this;
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(function() {
			//timer = null;
			$("#QueryTcpPingDefine").jqGrid('filterInput', self.value);
		}, 0);
	});
	//			
	////--------------------------------------------tab按ID查询-----------------------------------------------------	
	function showChildGrid(parentRowID, parentRowKey) {
		var childGridID = parentRowID + "_table";
		var childGridPagerID = parentRowID + "_pager";
		childGridKey = $("#QueryTcpPingGrid").getCell(parentRowKey, 'taskid');
		// send the parent row primary key to the server so that we know which grid to show
		var childGridURL = IP + "QueryTcpPingDataDetail?callback=?&session=" + session + "&taskid=" + childGridKey;
		console.log(childGridURL);
		//childGridURL = childGridURL + "&parentRowID=" + encodeURIComponent(parentRowKey)
		// add a table and pager HTML elements to the parent grid row - we will render the child grid here
		$('#' + parentRowID).append('<div style=' + "width:300px" + '><table id=' + childGridID + '></table><div id=' + childGridPagerID + ' class=scroll></div></div>');
		$("#" + childGridID).jqGrid({
			url: childGridURL,
			mtype: "Get",
			datatype: "jsonp",
			page: 1,
			colModel: [{
				label: '设备ID',
				name: 'taskid',
				width: 100
			}, {
				label: '域名',
				name: 'domain',
				width: 120
			}, {
				label: 'tclass',
				name: 'tclass',
				width: 100
			}, {
				label: '值',
				name: 'number',
				width: 75
			}, {
				label: '类型',
				name: 'type',
				width: 75
			}, {
				label: 'IP',
				name: 'ip',
				width: 75
			}],
			loadonce: true,
			rowNum: 10,
			height: '100%',
			pager: "#" + childGridPagerID
		});
	}
})